﻿using System;
using System.Linq;
using System.Runtime.InteropServices;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Derbster.Domain.Test
{
    [TestClass]
    public class JamStarPassUnitTest : ContractUnitTest
    {
        private Match _testMatch;
        private CommandManager _commandManager;

        [TestInitialize]
        public void TestSetup()
        {
            _testMatch = TestHelper.CreateTestMatch(true, true, "TestMatch");
            _commandManager = new CommandManager();
            _commandManager.Clear();
        }

        [TestMethod]
        public void TestMethodFirstStarPass()
        {
            _testMatch.BeginMatch();
            _testMatch.BeginPeriod();
            var player = _testMatch.FirstTeamRoster.Players.Skip(1).First();
            var player2 = _testMatch.FirstTeamRoster.Players.Skip(3).First();
            _testMatch.CurrentJam.AddFirstTeamJammer(player);
            _testMatch.CurrentJam.AddFirstTeamPivot(player2);
            _testMatch.CurrentJam.FirstTeamStarPassed();
            Assert.IsTrue(_testMatch.CurrentJam.FirstTeamLineup.StarPassed);
            Assert.IsTrue(_testMatch.CurrentJam.FirstTeamScoring.StarPassed);
            _commandManager.Undo();
            Assert.IsFalse(_testMatch.CurrentJam.FirstTeamLineup.StarPassed);
            Assert.IsFalse(_testMatch.CurrentJam.FirstTeamScoring.StarPassed);
            _commandManager.Redo();
            Assert.IsTrue(_testMatch.CurrentJam.FirstTeamLineup.StarPassed);
            Assert.IsTrue(_testMatch.CurrentJam.FirstTeamScoring.StarPassed);
        }

        [TestMethod]
        public void TestMethodSecondStarPass()
        {
            _testMatch.BeginMatch();
            _testMatch.BeginPeriod();
            var player = _testMatch.SecondTeamRoster.Players.Skip(1).First();
            var player2 = _testMatch.SecondTeamRoster.Players.Skip(3).First();
            _testMatch.CurrentJam.AddSecondTeamJammer(player);
            _testMatch.CurrentJam.AddSecondTeamPivot(player2);
            _testMatch.CurrentJam.SecondTeamStarPassed();
            Assert.IsTrue(_testMatch.CurrentJam.SecondTeamLineup.StarPassed);
            Assert.IsTrue(_testMatch.CurrentJam.SecondTeamScoring.StarPassed);
            _commandManager.Undo();
            Assert.IsFalse(_testMatch.CurrentJam.SecondTeamLineup.StarPassed);
            Assert.IsFalse(_testMatch.CurrentJam.SecondTeamScoring.StarPassed);
            _commandManager.Redo();
            Assert.IsTrue(_testMatch.CurrentJam.SecondTeamLineup.StarPassed);
            Assert.IsTrue(_testMatch.CurrentJam.SecondTeamScoring.StarPassed);
        }

        [TestMethod]
        public void TestMethodFirstStarPassNoPivot()
        {
            bool throws = true;

            _testMatch.BeginMatch();
            _testMatch.BeginPeriod();
            var player = _testMatch.FirstTeamRoster.Players.Skip(1).First();
            var player2 = _testMatch.FirstTeamRoster.Players.Skip(3).First();
            _testMatch.CurrentJam.AddFirstTeamJammer(player);
            _testMatch.CurrentJam.AddFirstTeamBlocker(player2);
            try
            {
                _testMatch.CurrentJam.FirstTeamStarPassed();
            }
            catch (Exception)
            {
                throws = true;
            }
            Assert.IsTrue(throws);
        }

        [TestMethod]
        public void TestMethodSecondStarPassNoPivot()
        {
            bool throws = true;

            _testMatch.BeginMatch();
            _testMatch.BeginPeriod();
            var player = _testMatch.SecondTeamRoster.Players.Skip(1).First();
            var player2 = _testMatch.SecondTeamRoster.Players.Skip(3).First();
            _testMatch.CurrentJam.AddSecondTeamJammer(player);
            _testMatch.CurrentJam.AddSecondTeamBlocker(player2);
            try
            {
                _testMatch.CurrentJam.SecondTeamStarPassed();
            }
            catch (Exception)
            {
                throws = true;
            }
            Assert.IsTrue(throws);
        }
    }
}
